package com.bocai.controller;

import com.bocai.bean.CardInfo;
import com.bocai.bean.TAdInfo;
import com.bocai.service.IAdInfoService;
import com.bocai.utils.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("adInfo")
public class AdInfoController {
    @Autowired
    private IAdInfoService iAdInfoService;

    @ApiOperation(value = "查询轮播广告", notes = "查询轮播广告")
    @RequestMapping(value = "/findAllAd", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findAllAdInfo() {
        ResponseData data = new ResponseData();
        data.setCodeEnum(CodeEnum.SUCCESS);
        data.setResults(iAdInfoService.selectAdd());
        return data;
    }

    @ApiOperation(value = "客户端查询轮播广告", notes = "客户端查询轮播广告")
    @RequestMapping(value = "/findAdInfoAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findAdInfoAll() {
        ResponseData data = new ResponseData();
        data.setCodeEnum(CodeEnum.SUCCESS);
        data.setResults(iAdInfoService.selectAddTwo());
        return data;
    }

    @ApiOperation(value = "添加轮播广告", notes = "")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "title", dataType = "string", required = true, value = "标题", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "bannerFlag", dataType = "int", required = true, value = "轮播图类型", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "adImage", dataType = "string", required = true, value = "图片链接地址", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "url", dataType = "string", required = true, value = "跳转地址", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "eventId", dataType = "int", required = true, value = "热门比赛Id", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "staTime", dataType = "string", required = true, value = "开始时间", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "endTime", dataType = "string", required = true, value = "结束时间", defaultValue = ""),
    })
    @RequestMapping(value = "/insertAd", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData insertAd(String title, Integer bannerFlag, String adImage, String url, Integer eventId, String staTime, String endTime) {
        ResponseData data = new ResponseData();
        TAdInfo tAdInfo = new TAdInfo();
        tAdInfo.setAdImage(adImage);
        tAdInfo.setAdUrl(url);
        tAdInfo.setTitle(title);
        if (null != staTime && null != endTime) {
            try {
                tAdInfo.setStaTime(DateUtils.parse(staTime, DateUtils.YYYY_MM_DD));
                tAdInfo.setEndTime(DateUtils.parse(endTime, DateUtils.YYYY_MM_DD));
            } catch (Exception e) {
                data.setCodeEnum(CodeEnum.CODE_90002);
                return data;
            }
        } else {
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }
        tAdInfo.setAdStatus(1);
        tAdInfo.setIsMatch(bannerFlag);
        if (bannerFlag == 1) {
            tAdInfo.setEventId(eventId);
        }
        if (iAdInfoService.insertAdInfo(tAdInfo) > 0) {
            data.setCodeEnum(CodeEnum.SUCCESS);
            return data;
        }

        data.setCodeEnum(CodeEnum.CODE_1);
        return data;
    }

    @ApiOperation(value = "修改广告")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "adId", dataType = "integer", value = "广告id"),
            @ApiImplicitParam(name = "adImage", dataType = "string", value = "广告图片"),
            @ApiImplicitParam(name = "adUrl", dataType = "string", value = "广告地址"),
            @ApiImplicitParam(name = "adStatus", dataType = "integer", value = "广告状态1:启用;0:禁用")
    })
    @RequestMapping(value = "/updateAd", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData updateAd(Integer adId, String adImage, String adUrl, Integer adStatus) {

        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{adId});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        TAdInfo adInfo = new TAdInfo();
        adInfo.setAdId(adId);
        adInfo.setAdImage(adImage);
        adInfo.setAdUrl(adUrl);
        adInfo.setAdStatus(adStatus);

        if (iAdInfoService.updateAdInfo(adInfo) > 0) {
            data.setCodeEnum(CodeEnum.SUCCESS);
            return data;
        }

        data.setCodeEnum(CodeEnum.CODE_1);
        return data;
    }
}
